home *** CD-ROM | disk | FTP | other *** search
/ Collection of Internet / Collection of Internet.iso / faq / news / perl_faq / part4 < prev   
Text File  |  1993-10-01  |  13KB  |  389 lines

  1. Newsgroups: comp.lang.perl,news.answers
  2. Path: senator-bedfellow.mit.edu!bloom-beacon.mit.edu!spool.mu.edu!howland.reston.ans.net!pipex!uunet!boulder!wraeththu.cs.colorado.edu!tchrist
  3. From: Tom Christiansen <tchrist@cs.Colorado.EDU>
  4. Subject: Perl Frequently Asked Questions, part 4 of 4
  5. Message-ID: <CE9Brq.29B@Colorado.EDU>
  6. Followup-To: comp.lang.perl
  7. Originator: tchrist@wraeththu.cs.colorado.edu
  8. Sender: news@Colorado.EDU (USENET News System)
  9. Organization: University of Colorado at Boulder
  10. Date: Sat, 2 Oct 1993 06:38:13 GMT
  11. Approved: news-answers-request@MIT.Edu
  12. Expires: Wed, 1 Dec 1993 12:00:00 GMT
  13. Lines: 373
  14. Xref: senator-bedfellow.mit.edu comp.lang.perl:20585 news.answers:13123
  15.  
  16. Archive-name: perl-faq/part4
  17. Version: $Id: perl-tech2,v 1.1 93/10/02 00:27:03 tchrist Exp Locker: tchrist $
  18.  
  19. This posting contains answers to the following technical questions
  20. regarding Perl:
  21.  
  22. 2.29) Why can't my perl program read from STDIN after I gave it ^D (EOF) ?
  23. 2.30) Do I always/never have to quote my strings or use semicolons?
  24. 2.31) How can I translate tildes in a filename?
  25. 2.32) How can I convert my shell script to Perl?
  26. 2.33) What is variable suicide and how can I prevent it?
  27. 2.34) Can I use Perl regular expressions to match balanced text?
  28. 2.35) Can I use Perl to run a telnet or ftp session?
  29. 2.36) What does "Malformed command links" mean?
  30. 2.37) How can I set up a footer format to be used with write()?
  31. 2.38) Why does my Perl program keep growing in size?
  32. 2.39) Can I do RPC in Perl?
  33. 2.40) What's the difference between delete and undef with %tables?
  34. 2.41) How do I do a "tail -f" in Perl?
  35.  
  36.  
  37. 2.29) Why can't my perl program read from STDIN after I gave it ^D (EOF) ?
  38.  
  39.     Because some stdio's set error and eof flags that need clearing.
  40.  
  41.     Try keeping around the seekpointer and go there, like this:
  42.      $where = tell(LOG);
  43.      seek(LOG, $where, 0);
  44.  
  45.     If that doesn't work, try seeking to a different part of the file and
  46.     then back.  If that doesn't work, try seeking to a different part of
  47.     the file, reading something, and then seeking back.  If that doesn't
  48.     work, give up on your stdio package and use sysread.  You can't call
  49.     stdio's clearerr() from Perl, so if you get EINTR from a signal
  50.     handler, you're out of luck.  Best to just use sysread() from the
  51.     start for the tty.
  52.  
  53.  
  54. 2.30) Do I always/never have to quote my strings or use semicolons?
  55.  
  56.     You don't have to quote strings that can't mean anything else
  57.     in the language, like identifiers with any upper-case letters
  58.     in them.  Therefore, it's fine to do this:
  59.  
  60.     $SIG{INT} = Timeout_Routine;
  61.     or 
  62.  
  63.     @Days = (Sun, Mon, Tue, Wed, Thu, Fri, Sat, Sun);
  64.  
  65.     but you can't get away with this:
  66.  
  67.     $foo{while} = until;
  68.  
  69.     in place of 
  70.  
  71.     $foo{'while'} = 'until';
  72.  
  73.     The requirements on semicolons have been increasingly relaxed.  You no
  74.     longer need one at the end of a block, but stylistically, you're
  75.     better to use them if you don't put the curly brace on the same line:
  76.  
  77.     for (1..10) { print }
  78.  
  79.     is ok, as is
  80.  
  81.     @nlist = sort { $a <=> $b } @olist;
  82.  
  83.     but you probably shouldn't do this:
  84.     
  85.     for ($i = 0; $i < @a; $i++) {
  86.         print "i is $i\n"  # <-- oops!
  87.     } 
  88.  
  89.     because you might want to add lines later, and anyway, 
  90.     it looks funny. :-)
  91.  
  92.  
  93. 2.31) How can I translate tildes in a filename?
  94.  
  95.     Perl doesn't expand tildes -- the shell (ok, some shells) do.
  96.     The classic request is to be able to do something like:
  97.  
  98.     open(FILE, "~/dir1/file1");
  99.     open(FILE, "~tchrist/dir1/file1");
  100.  
  101.     which doesn't work.  (And you don't know it, because you 
  102.     did a system call without an "|| die" clause! :-)
  103.  
  104.     If you *know* you're on a system with the csh, and you *know*
  105.     that Larry hasn't internalized file globbing, then you could
  106.     get away with 
  107.  
  108.     $filename = <~tchrist/dir1/file1>;
  109.  
  110.     but that's pretty iffy.
  111.  
  112.     A better way is to do the translation yourself, as in:
  113.  
  114.     $filename =~ s#^~(\w+)(/.*)?$#(getpwnam($1))[7].$2#e;
  115.  
  116.     More robust and efficient versions that checked for error conditions,
  117.     handed simple ~/blah notation, and cached lookups are all reasonable
  118.     enhancements.
  119.  
  120.  
  121. 2.32) How can I convert my shell script to Perl?
  122.  
  123.     Larry's standard answer for this is to send your script to me (Tom
  124.     Christiansen) with appropriate supplications and offerings.  :-(
  125.     That's because there's no automatic machine translator.  Even if you
  126.     were, you wouldn't gain a lot, as most of the external programs would
  127.     still get called.  It's the same problem as blind translation into C:
  128.     you're still apt to be bogged down by exec()s.  You have to analyze
  129.     the dataflow and algorithm and rethink it for optimal speedup.  It's
  130.     not uncommon to see one, two, or even three orders of magnitude of
  131.     speed difference between the brute-force and the recoded approaches.
  132.  
  133.  
  134. 2.33) What is variable suicide and how can I prevent it?
  135.  
  136.     Variable suicide is a nasty sideeffect of dynamic scoping and
  137.     the way variables are passed by reference.  If you say
  138.  
  139.     $x = 17;
  140.     &munge($x);
  141.     sub munge {
  142.         local($x);
  143.         local($myvar) = $_[0];
  144.         ...
  145.     } 
  146.  
  147.     Then you have just clubbered $_[0]!  Why this is occurring 
  148.     is pretty heavy wizardry: the reference to $x stored in 
  149.     $_[0] was temporarily occluded by the previous local($x)
  150.     statement (which, you're recall, occurs at run-time, not
  151.     compile-time).  The work around is simple, however: declare
  152.     your formal parameters first:
  153.  
  154.     sub munge {
  155.         local($myvar) = $_[0];
  156.         local($x);
  157.         ...
  158.     }
  159.  
  160.     That doesn't help you if you're going to be trying to access
  161.     @_ directly after the local()s.  In this case, careful use
  162.     of the package facility is your only recourse.
  163.  
  164.     Another manifestation of this problem occurs due to the
  165.     magical nature of the index variable in a foreach() loop.
  166.  
  167.     @num = 0 .. 4;
  168.     print "num begin  @num\n";
  169.     foreach $m (@num) { &ug }
  170.     print "num finish @num\n";
  171.     sub ug {
  172.         local($m) = 42;
  173.         print "m=$m  $num[0],$num[1],$num[2],$num[3]\n";
  174.     }
  175.     
  176.     Which prints out the mysterious:
  177.  
  178.     num begin  0 1 2 3 4
  179.     m=42  42,1,2,3
  180.     m=42  0,42,2,3
  181.     m=42  0,1,42,3
  182.     m=42  0,1,2,42
  183.     m=42  0,1,2,3
  184.     num finish 0 1 2 3 4
  185.  
  186.     What's happening here is that $m is an alias for each 
  187.     element of @num.  Inside &ug, you temporarily change
  188.     $m.  Well, that means that you've also temporarily 
  189.     changed whatever $m is an alias to!!  The only workaround
  190.     is to be careful with global variables, using packages,
  191.     and/or just be aware of this potential in foreach() loops.
  192.  
  193.     The perl5 static autos via "my" will not have this problem.
  194.  
  195.  
  196. 2.34) Can I use Perl regular expressions to match balanced text?
  197.  
  198.     No, or at least, not by the themselves.
  199.  
  200.     Regexps just aren't powerful enough.  Although Perl's patterns aren't
  201.     strictly regular because they do backtracking (the \1 notation), you
  202.     still can't do it.  You need to employ auxiliary logic.  A simple
  203.     approach would involve keeping a bit of state around, something 
  204.     vaguely like this (although we don't handle patterns on the same line):
  205.  
  206.     while(<>) {
  207.         if (/pat1/) {
  208.         if ($inpat++ > 0) { warn "already saw pat1" } 
  209.         redo;
  210.         } 
  211.         if (/pat2/) {
  212.         if (--$inpat < 0) { warn "never saw pat1" } 
  213.         redo;
  214.         } 
  215.     }
  216.  
  217.     A rather more elaborate subroutine to pull out balanced and possibly
  218.     nested single chars, like ` and ', { and }, or ( and ) can be found
  219.     on convex.com in /pub/perl/scripts/pull_quotes.
  220.  
  221.  
  222. 2.35) Can I use Perl to run a telnet or ftp session?
  223.  
  224.     Sure, you can connect directly to them using sockets, or you can run a
  225.     session on a pty.  In either case, Randal's chat2 package, which is
  226.     distributed with the perl source, will come in handly.  It address
  227.     much the same problem space as Don Libes's expect package does.  Two
  228.     examples of using managing an ftp session using chat2 can be found on
  229.     convex.com in /pub/perl/scripts/ftp-chat2.shar .
  230.  
  231.     Caveat lector: chat2 is documented only by example, may not run on
  232.     System V systems, and is subtly machine dependent both in its ideas
  233.     of networking and in pseudottys.
  234.  
  235.  
  236. 2.36) What does "Malformed command links" mean?
  237.  
  238.     This is a bug in 4.035.  While in general it's merely a cosmetic
  239.     problem, it often comanifests with a highly undesirable coredumping
  240.     problem.  Programs known to be affected by the fatal coredump include
  241.     plum and pcops.  Since perl5 is pretty much a total rewrite, we can
  242.     count on it being fixed then, but if anyone tracks down the coredump
  243.     problem before then, a significant portion of the Perl world would
  244.     rejoice.
  245.  
  246.  
  247. 2.37) How can I set up a footer format to be used with write()?
  248.  
  249.     While the $^ variable contains the name of the current header format,
  250.     there is no corresponding mechanism to automatically do the same thing
  251.     for a footer.  Not knowing how big a format is going to be until you
  252.     evaluate it is one of the major problems.
  253.  
  254.     If you have a fixed-size footer, you can get footers by checking for
  255.     line left on page ($-) before each write, and printing the footer
  256.     yourself if necessary.
  257.  
  258.     Another strategy is to open a pipe to yourself, using open(KID, "|-")
  259.     and always write()ing to the KID, who then postprocesses its STDIN to
  260.     rearrange headers and footers however you like.  Not very convenient,
  261.     but doable.
  262.  
  263.  
  264. 2.38) Why does my Perl program keep growing in size?
  265.  
  266.     While there may be a real memory leak in the Perl source code or even
  267.     whichever malloc() you're using, common causes are incomplete eval()s
  268.     or local()s in loops.
  269.  
  270.     An eval() which terminates in error due to a failed parsing 
  271.     will leave a bit of memory unusable.
  272.  
  273.     A local() inside a loop:
  274.  
  275.     for (1..100) {
  276.         local(@array);
  277.     } 
  278.  
  279.     will build up 100 versions of @array before the loop is done.
  280.     The work-around is:
  281.  
  282.     local(@array);
  283.     for (1..100) {
  284.         undef @array;
  285.     } 
  286.  
  287.     Larry reports that this behavior is fixed for perl5.
  288.  
  289.  
  290. 2.39) Can I do RPC in Perl?
  291.  
  292.     Yes, you can, since Perl has access to sockets.  An example of the rup
  293.     program written in Perl can be found in the script ruptime.pl at
  294.     the scripts archive on coombs.anu.edu.au .   I warn you, however,
  295.     that it's not a pretty site, as it's used nothing from h2ph or c2ph, 
  296.     so everything is utterly hard-wired.
  297.  
  298.  
  299. 2.40) What's the difference between delete and undef with %tables?
  300.  
  301.     Pictures help...  here's the %ary table:
  302.  
  303.           keys  values
  304.         +------+------+
  305.         |  a   |  3   |
  306.         |  x   |  7   |
  307.         |  d   |  0   |
  308.         |  e   |  2   |
  309.         +------+------+
  310.  
  311.     And these conditions hold
  312.  
  313.         $ary{'a'}                       is true
  314.         $ary{'d'}                       is false
  315.         defined $ary{'d'}               is true
  316.         defined $ary{'a'}               is true
  317.         grep ($_ eq 'a', keys %ary)     is true
  318.  
  319.     If you now say 
  320.  
  321.         undef $ary{'a'}
  322.  
  323.     your table now reads:
  324.         
  325.  
  326.           keys  values
  327.         +------+------+
  328.         |  a   | undef|
  329.         |  x   |  7   |
  330.         |  d   |  0   |
  331.         |  e   |  2   |
  332.         +------+------+
  333.  
  334.     and these conditions now hold; changes in caps:
  335.  
  336.         $ary{'a'}                       is FALSE
  337.         $ary{'d'}                       is false
  338.         defined $ary{'d'}               is true
  339.         defined $ary{'a'}               is FALSE
  340.         grep ($_ eq 'a', keys %ary)     is true
  341.  
  342.     Notise the last two: you have an undef value, but a defined key!
  343.  
  344.     Now, consider this:
  345.  
  346.         delete $ary{'a'}
  347.  
  348.     your table now reads:
  349.  
  350.           keys  values
  351.         +------+------+
  352.         |  x   |  7   |
  353.         |  d   |  0   |
  354.         |  e   |  2   |
  355.         +------+------+
  356.  
  357.     and these conditions now hold; changes in caps:
  358.  
  359.         $ary{'a'}                       is false
  360.         $ary{'d'}                       is false
  361.         defined $ary{'d'}               is true
  362.         defined $ary{'a'}               is false
  363.         grep ($_ eq 'a', keys %ary)     is FALSE
  364.  
  365.     See, the whole entry is gone!
  366.  
  367. 2.41) How do I do a "tail -f" in Perl?
  368.  
  369.     Larry says that the solution is to put a call to seek in yourself. 
  370.     First try
  371.  
  372.         seek(GWFILE, 0, 1);
  373.  
  374.     If that doesn't work (depends on your stdio implementation), then
  375.     you need something more like this:
  376.  
  377.  
  378.     for(;;) {
  379.         for ($curpos = tell(GWFILE); $_ = <GWFILE>; $curpos = tell(GWFILE)) {
  380.            search for some stuff and put it into files
  381.         }
  382.         sleep for a while 
  383.         seek(GWFILE, $curpos, 0);
  384.     }
  385. -- 
  386.     Tom Christiansen      tchrist@cs.colorado.edu       
  387.             Consultant
  388.     Boulder Colorado  303-444-3212
  389.